#ifndef U2Singleton_H
#define U2Singleton_H

#include "U2PreRequest.h"


#if U2_COMPILER == U2_COMPILER_MSVC
#   pragma warning (disable : 4661)
#endif


U2EG_NAMESPACE_BEGIN


template <typename T> class U2Singleton
{
protected:

    static T*           s_pSingleton;

public:

    U2Singleton( void )
    {
        assert( !s_pSingleton );
#if defined( _MSC_VER ) && _MSC_VER < 1200	 
        int offset = (int)(T*)1 - (int)(U2Singleton <T>*)(T*)1;
        s_pSingleton = (T*)((int)this + offset);
#else
        s_pSingleton = static_cast< T* >( this );
#endif
    }

    ~U2Singleton( void )
    {
        assert( s_pSingleton );
        s_pSingleton = 0;
    }

    static T& getSingleton( void )
    {
        assert( s_pSingleton );
        return ( *s_pSingleton );
    }

    static T* getSingletonPtr( void )
    {
        return s_pSingleton;
    }

};

U2EG_NAMESPACE_END

#endif